package org.easyexcel.Test;


import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.concurrent.*;

public class MyPool {
}

class pool {

    public static void main(String[] args) {

//        Sync1();
        // 创建一个包含3个线程的线程池
//        MyFix();
//        MySing();
//        MyShcedu();

        scheduleWeeklyTask();

    }


    public static void scheduleWeeklyTask() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime time = now.withHour(16).withMinute(50).withSecond(0).withNano(0);
//           //如果 当前时间>本周周四，必须找到下周周四
        if (now.compareTo(time) > 0) {
            time = time.plusWeeks(1);
        }
        System.out.println(time);// initailpelay 代表当前时间和周四的时间差// period 一周的间隔时间
        long initailDelay = Duration.between(now, time).toMillis();
        long period = 1000 * 60 * 60 * 24 * 7;
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
         pool.scheduleAtFixedRate(() -> {
            System.out.println("running...");
        }, initailDelay, period, TimeUnit.MILLISECONDS);


    }

    private static void MyShcedu() {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        pool.scheduleAtFixedRate(() -> {
            System.out.println("1 " + "当前线程：" + Thread.currentThread().getName());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, 0, 1, TimeUnit.SECONDS);
        pool.scheduleAtFixedRate(() -> {
            System.out.println("2 " + "当前线程：" + Thread.currentThread().getName());
        }, 0, 1, TimeUnit.SECONDS);
    }

    private static void MySing() {
        ExecutorService pool = Executors.newSingleThreadExecutor();
        pool.execute(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getName());
            int i = 1 / 0;
        });
        pool.execute(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getName());
        });
        pool.execute(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getName());
        });
        pool.shutdown();
    }

    private static void MyFix() {
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        // 提交5个任务给线程池执行
        for (int i = 0; i < 5; i++) {
            int taskNumber = i + 1;
            executorService.submit(() -> {
                System.out.println("任务 " + taskNumber + " 正在由线程 " + Thread.currentThread().getName() + " 执行");
                try {
                    // 模拟任务耗时操作
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务 " + taskNumber + " 执行完毕");
            });
        }
        // 关闭线程池
        executorService.shutdown();
    }

//    }

//    private static void Sync1() {
//        SynchronousQueue integs = new SynchronousQueue();
//        new Thread(() -> {
//            try {
//                integs.put(1);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println("put 1");
//        }, "t1").start();
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        new Thread(() -> {
//            try {
//                integs.put(2);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println("put 2");
//        }, "t2").start();
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        new Thread(() -> {
//            try {
//                Object take = integs.take();
//                System.out.println("take"+take);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//
//        }, "t3").start();
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        new Thread(() -> {
//            try {
//                Object take = integs.take();
//                System.out.println("take"+take);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//
//        }, "t4").start();
//    }
//

}

